package com.hyjx.framework.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

/**
 * 时间处理帮助类
 * wangjd
 */
public class DateHelper {

    /**
     * 将 类似 6.5六月五号，转换成data追加年
     *
     * @param str
     * @param year
     * @param format
     * @return
     */
    public static Date monthToDate(String str, int year, String format) throws Exception {
        if (str == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Calendar cal = Calendar.getInstance();
        Date date = simpleDateFormat.parse(str);
        cal.setTime(date);
        cal.set(Calendar.YEAR, year);
        return cal.getTime();
    }

    /**
     * 返回年月日时分秒字串
     *
     * @param date
     * @return
     */
    public static String getDateTimeStr(Date date) {
        SimpleDateFormat lFormatTimestamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rtn2 = lFormatTimestamp.format(date);
        rtn2 = rtn2.substring(0, 4) + rtn2.substring(5, 7) + rtn2.substring(8, 10)
                + rtn2.substring(11, 13) + rtn2.substring(14, 16) + rtn2.substring(17, 19);
        return rtn2;
    }

    /**
     * @param str
     * @param format
     * @return
     *
     * @throws Exception
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-6 下午2:13:09
     */
    public static Date toDate(String str, String format) {
        if (str == null || "".equals(str.trim())) {
            return null;
        }
        Date date = null;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        try {
            date = simpleDateFormat.parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String toDate1(String str, String format) {
        if (str == null || "".equals(str.trim())) {
            return null;
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        String sDate = null;
        try {
            Date date = sdf1.parse(str);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
            sDate = simpleDateFormat.format(date);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        return sDate;
    }

    public static Date toDate2(String str) {
        if (str == null || "".equals(str.trim())) {
            return null;
        }
        SimpleDateFormat sdf1 = new SimpleDateFormat("EEE MMM dd HH:mm:ss Z yyyy", Locale.UK);
        Date date = null;
        try {
            date = sdf1.parse(str);
        } catch (ParseException e1) {
            e1.printStackTrace();
        }

        return date;
    }

    /**
     * 将日期转化为字符串型
     *
     * @param date
     * @param format
     * @return
     */
    public static String toString(Date date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);
    }


    /**
     * 格式化当前系统时间
     *
     * @return
     */
    public static Date formatDate(Date date, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH);
        int s = c.get(Calendar.DATE);
        String d = String.valueOf(y) + "-" + String.valueOf(m) + "-" + String.valueOf(s);
        return toDate(d, format);
    }


    /**
     * 格式化当前系统时间
     *
     * @return
     */
    public static Date formatDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONTH) + 1;
        String d = String.valueOf(y) + "-" + String.valueOf(m) + "-1";
        return toDate(d, "yyyy-MM-dd");
    }

    /**
     * 获得当前系统时间
     *
     * @return
     */
    public static String getSystemDateNow(String format) {
        Date date = new Date();
        return toString(date, format);
    }
    
    /**
     * 获取系统时间 格式yyyy-MM-dd hh:mm:ss
     *
     * @return
     *
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-13 上午11:16:05
     */
    public static String getSimpleSystemDate() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        return sdf.format(date);
    }

    /**
     * 获取系统时间 格式yyyy-MM-dd
     *
     * @return
     *
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-13 上午11:16:29
     */
    public static String getSimpleSystemDay() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(date);
    }
    
    /**
     * 获取系统时间 毫秒数
     *
     * @return
     *
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-13 上午11:16:51
     */
    public static Date getSystemDate() {
        Calendar c = Calendar.getInstance();
        return c.getTime();
    }
    
    
    /**
     * 获取系统时间 提前或者延后的秒数  +5：延后5秒  -5：提前五秒
     *
     * @param step 提前或者延后的毫秒
     * @return
     *
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-13 上午11:17:45
     */
    public static Date getSystemDate(int step) {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.SECOND, step);
        return c.getTime();
    }
    
    /**
     * 获得日期年份
     *
     * @param date
     * @return
     */
    public static int getYear(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int year = c.get(Calendar.YEAR);
        return year;
    }
    
    /**
     * 获得日期月份
     *
     * @param date
     * @return
     */
    public static int getMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        int month = c.get(Calendar.MONTH) + 1;
        return month;
    }
    
    /**
     * 获取系统年份
     *
     * @return
     *
     * @author hyteddy hyteddy@163.com
     * @date 2014-8-13 上午11:17:17
     */
    public static int getSystemYear() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        return year;
    }


    /**
     * @param date
     * @return 日期字符串
     *
     * @说明：获取上个月的日期
     */
    public static String getLastMonth(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return toString(c.getTime(), "yyyy-MM-dd");
    }
    
    public static String getLastMonth(Date date, String format) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return toString(c.getTime(), format);
    }
    
    /**
     * @param date
     * @return 日期对象
     *
     * @说明：获取上个月的日期
     */
    public static Date getPreviousMonthDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return formatDate(c.getTime());
    }
    
    /**
     * 张瑞华 新加
     *
     * @param date
     * @return 日期对象
     *
     * @说明：获取本月的日期
     */
    public static Date getthisMonthDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 0);
        return formatDate(c.getTime());
    }

    /**
     * @param date
     * @return
     *
     * @说明：获取下个月的日期
     */
    public static Date getNextMonthDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, 1);
        return formatDate(c.getTime());
    }


    public static Date beforeDay(Date date, int day) {
        Calendar cd = Calendar.getInstance();
        cd.setTime(date);
        cd.add(Calendar.DATE, -day);
        return cd.getTime();
    }


    public static String getLastDayOfMonth(String year, String month) {
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.parseInt(year));
        // 月，因为Calendar里的月是从0开始，所以要-1
        cal.set(Calendar.MONTH, Integer.parseInt(month) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);
        return String.valueOf(cal.get(Calendar.DAY_OF_MONTH));// 获得月末是几号
    }

    /**
     * 根据出生日期算年龄
     *
     * @param date
     */
    public static int getAgeByBirth(Date birthDay) {
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }

        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH) + 1;
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        
        cal.setTime(birthDay);
        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);
        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                //monthNow==monthBirth 
                if (dayOfMonthNow < dayOfMonthBirth) {
                    age--;
                }
            } else {
                //monthNow>monthBirth 
                age--;
            }
        }

        return age;
    }

    public static Date getLastDayDate(Date relaDate) {
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.valueOf(toString(relaDate, "yyyy")));
        // 月，因为Calendar里的月是从0开始，所以要-1
        cal.set(Calendar.MONTH, Integer.valueOf(toString(relaDate, "MM")) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);
        // 月份加一，得到下个月的一号
        cal.add(Calendar.MONTH, 1);
        // 下一个月减一为本月最后一天
        cal.add(Calendar.DATE, -1);

        return cal.getTime();
    }

    public static Date getFistDayDate(Date relaDate) {
        Calendar cal = Calendar.getInstance();
        // 年
        cal.set(Calendar.YEAR, Integer.valueOf(toString(relaDate, "yyyy")));
        // 月，因为Calendar里的月是从0开始，所以要-1
        cal.set(Calendar.MONTH, Integer.valueOf(toString(relaDate, "MM")) - 1);
        // 日，设为一号
        cal.set(Calendar.DATE, 1);

        return cal.getTime();
    }

    /**
     * 取某日期的相关日期
     *
     * @param relaDate
     * @return
     */
    public static Date getRelaDate(Date date, Integer relaDay) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.DATE, relaDay);
        return cal.getTime();
    }

    /**
     * 比较两个日期差几天，只比较日期（不含时分秒）D2-D1
     *
     * @param relaDate
     * @return
     */
    public static Long daysCompare(Date d1, Date d2) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        String s1 = sdf.format(d1);
        String s2 = sdf.format(d2);

        Long result = 0L;
        try {
            Date date1 = sdf.parse(s1);
            Date date2 = sdf.parse(s2);

            long diff = date2.getTime() - date1.getTime();

            result = diff / (1000 * 60 * 60 * 24);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 当前日期同指定日期差几天，只比较日期（不含时分秒）
     *
     * @param relaDate
     * @return
     */
    public static Long nowComparesDate(Date date) {
        return daysCompare(date, new Date());
    }
}